package com.wzq.permission.center.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wzq.permission.center.mapper.ApplicationMapper;
import com.wzq.permission.center.mapper.MenuMapper;
import com.wzq.permission.client.model.MenuTypeEnum;
import com.wzq.permission.client.model.Pager;
import com.wzq.permission.client.model.admin.ApplicationModel;
import com.wzq.permission.client.model.admin.MenuModel;
import com.wzq.permission.client.service.ApplicationService;

/**
 * 应用系统服务
 * Project:wzq-permission-center 
 * @Author chenjianfei
 * create Time:2015年12月31日 10：21：30
 *
 */
@Service
@Transactional
public class ApplicationServiceImpl implements ApplicationService {
	protected final Logger logger = LogManager.getLogger(getClass());
	@Autowired
    private ApplicationMapper mapper;
	@Autowired
	private MenuMapper menuMapper;
	
	@Override
	public void add(ApplicationModel application) {
		application.setDeleted("0");
		mapper.add(application);
		MenuModel menuModel = new MenuModel();
		menuModel.setMenuName(application.getApplicationName());
		menuModel.setApplicationId(1l);
		menuModel.setCreateUser(application.getCreateUser());
		menuModel.setUpdateUser(application.getUpdateUser());
		menuModel.setMenuType(MenuTypeEnum.MENU.getType());
		menuModel.setDeleted("0");
		menuModel.setParentId(-1l);
		menuModel.setSerialNum(menuMapper.getMaxSerialNumByApplicationId(1l,MenuTypeEnum.MENU.getType(), -1l)+1);
		menuMapper.add(menuModel);
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(ApplicationServiceImpl.class.getResource("/wzq-permission-center/conf/default_menu.xml"));
			Element menus=document.getRootElement();
			int serial=1;
			for(Iterator i = menus.elementIterator(); i.hasNext();){
				Element menu = (Element) i.next();
				String menuName = menu.attributeValue("menuName");
				String linkUrl = menu.attributeValue("linkUrl");
				MenuModel menuTemp = new MenuModel();
				menuTemp.setParentId(menuModel.getMenuId());
				menuTemp.setMenuName(menuName);
				menuTemp.setApplicationId(1l);
				menuTemp.setCreateUser(application.getCreateUser());
				menuTemp.setUpdateUser(application.getUpdateUser());
				menuTemp.setMenuType(MenuTypeEnum.MENU.getType());
				menuTemp.setSerialNum(serial);
				menuTemp.setDeleted("0");
				menuTemp.setLinkUrl(linkUrl+"?applicationId="+application.getApplicationId());
				menuMapper.add(menuTemp);
				Element children = menu.element("children");
				List<MenuModel> childrenList = new ArrayList<MenuModel>();
				int childrenSerial=1;
				for(Iterator j = children.elementIterator(); j.hasNext();){
					Element childrenMenu = (Element) j.next();
					String childrenMenuName = childrenMenu.attributeValue("menuName");
					String childrenlinkUrl = childrenMenu.attributeValue("linkUrl");
					MenuModel childrenMenuModel = new MenuModel();
					childrenMenuModel.setParentId(menuTemp.getMenuId());
					childrenMenuModel.setMenuName(childrenMenuName);
					childrenMenuModel.setApplicationId(1l);
					childrenMenuModel.setCreateUser(application.getCreateUser());
					childrenMenuModel.setUpdateUser(application.getUpdateUser());
					childrenMenuModel.setMenuType(MenuTypeEnum.OPERATE.getType());
					childrenMenuModel.setSerialNum(childrenSerial);
					childrenMenuModel.setDeleted("0");
					childrenMenuModel.setLinkUrl(childrenlinkUrl+"?applicationId="+application.getApplicationId());
					childrenList.add(childrenMenuModel);
					childrenSerial++;
				}
				menuMapper.addBatch(childrenList);
				serial++;
			}
		} catch (DocumentException e) {
			logger.error("读取菜单配置失败", e);
		}
	}

	@Override
	public void update(ApplicationModel application) {
		mapper.update(application);
	}

	@Override
	public void updateBySelective(ApplicationModel application) {
		mapper.updateBySelective(application);
	}

	@Override
	public void delete(Object id) {
		mapper.delete(id);
	}

	@Override
	public int queryByCount(ApplicationModel application) {
		return mapper.queryByCount(application);
	}

	@Override
	public List<ApplicationModel> queryList(ApplicationModel application,Pager pager) {
		if(StringUtils.isNotEmpty(application.getApplicationName())){
			application.setApplicationName("%"+application.getApplicationName()+"%");
		}
		return mapper.queryList(application,pager);
	}

	@Override
	public ApplicationModel queryById(Object id) {
		return mapper.queryById(id);
	}

	@Override
	public int logicDelete(Long[] applicationIds) {
		return mapper.logicDelete(applicationIds);
	}

	@Override
	public Long getApplicationIdByKey(String applicationKey) {
		return mapper.getApplicationIdByKey(applicationKey);
	}

	@Override
	public int isExistApplication(String applicationKey) {
		return mapper.isExistApplication(applicationKey);
	}
	
	
}
